<?php

/*
*
* BEGIN team_management_ui.php
*
*
* Dazzler Team Management Uis
* @Description: The class creates all the UIs for the team management
* @Authors: Salman, Wits University
* @Copyright: © Wits University 2011 - All Rights Reserved
* @License: BSD License (http://www.linfo.org/bsdlicense.html)
*
*
* BEGIN CHANGELOG
* Developers working on thee files are REQUIRED to maintain this CHANGELOG.
* Please ensure you follow the Changelog Guidelines: http://code.google.com/p/dazzler/wiki/DazzlerDevelopGuideline?ts=1315476570&updated=DazzlerDevelopGuideline
* !==================================================================================================================================================================
*
* VERSION 1.0.0
*	+ File Created, Salman, 24 September 2011
*
* ==================================================================================================================================================================!
* END CHANGELOG
*/


/*******************************************************************************************************************************************************************
*
* BEGIN Direct Call Check
*
/******************************************************************************************************************************************************************/
global $doNotCallThisScriptDirectly;
if ($doNotCallThisScriptDirectly != 1)
{
	/* Read the ini language file */
	$ini_lang_array = array();
	$clientlang = explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
	$lang_ini_array = (file_exists("lang/" . trim($clientlang[0]) . ".ini")) ? parse_ini_file("lang/" . trim($clientlang[0]) . ".ini", true) : parse_ini_file("lang/en.ini", true);
	die ($lang_ini_array['ERRORS']['doNotCallThisScriptDirectly']);
}
/*******************************************************************************************************************************************************************
*
* END Direct Call Check
*
/******************************************************************************************************************************************************************/

class team_management_ui extends baseClass
{

    private $htmlelementsbuilder;

    private $parsesiteconfig;

    public function  __construct()
    {
    $this->htmlelementsbuilder = new htmlelementsbuilder();
    $this->parsesiteconfig = new parsesiteconfig();
    parent::parseLanguageErrorLogFile();
    }

    private function buildAddTeamForm()
    {
        $AddTeamWindow = null;
        $AddTeamWindow .="<div style =' border: 1px solid transparent; padding: 7px' class='errorMessageDiv'> </div>";
        $AddTeamWindow .="<div class='responseDiv'> </div>";
        $postPath = $this->parsesiteconfig->getURLPath();
        $AddTeamWindow .= "<form id='addTeamForm' method='post' name='add team form'";
        $AddTeamWindow .= $this->htmlelementsbuilder->buildTextInput('teamName', null, 'teamName', TRUE, "Team Name", 'top') . "<br>";
        $AddTeamWindow .= $this->htmlelementsbuilder->buildTextInput('shortName', null, 'shortName', TRUE, "Short Name of Team's Country", 'top') . "<br>";
        $AddTeamWindow .="</form>";
        return $AddTeamWindow;
    }

 

    private function buildTeamRankingList()
    {
        $GameAdmin = new GameAdmin();
        $teamRankingsArray = $GameAdmin->getAllTeamsByRank();

        $unrankedTeamsListArray = array();
        $rankedTeamsListArray = array();
        foreach ($teamRankingsArray as $key => $team) {
            if ($team['TEAM_POOL_RANK'] < 1 || $team['TEAM_POOL_RANK'] == NULL)
            {
              $unrankedTeamListItem = new htmlcodemaker('li');
              $unrankedTeamListItem->set(array('id'=>$team['TEAM_POOL_NAME'],'class'=>'ui-state-default','text'=>$team['TEAM_POOL_NAME']));
              $unrankedTeamsListArray[] = $unrankedTeamListItem;
            }
            else
            {
              $rankedTeamListItem = new htmlcodemaker('li');
              $rankedTeamListItem->set(array('id'=>$team['TEAM_POOL_NAME'],'class'=>'ui-state-highlight','text'=>$team['TEAM_POOL_NAME']));
              $rankedTeamsListArray[] = $rankedTeamListItem;
            }
        }

      if (empty($rankedTeamsListArray)) {
              $rankedTeamListItem = new htmlcodemaker('li');
              $rankedTeamListItem->set(array('id'=>'ignoreMockList','class'=>'ui-state-highlight ui-state-disabled','text'=>"No Teams Are Currently Ranked."));
              $rankedTeamsListArray[] = $rankedTeamListItem;
}

      if (empty($rankedTeamsListArray)) {
              $unrankedTeamListItem = new htmlcodemaker('li');
              $unrankedTeamListItem->set(array('id'=>'ignoreMockList','class'=>'ui-state-highlight ui-state-disabled','text'=>"No Teams Are Currently Inserted."));
              $unrankedTeamsListArray[] = $rankedTeamListItem;
}
        $unrankedTeams = new htmlcodemaker('ul');
        $unrankedTeams->set(array("id"=>"unranked","class"=>"connectedSortable"));
        $unrankedTeams->inject($unrankedTeamsListArray);
        
        $rankedTeams = new htmlcodemaker('ul');
        $rankedTeams->set(array("id"=>"ranked","class"=>"connectedSortable"));
        $rankedTeams->inject($rankedTeamsListArray);

                return "<style>
	#unranked, #ranked { list-style-type: none; margin: 1; padding: 0; float: left; margin-right: 15px; }
	#unranked li, #ranked li { margin: 7px 7px 7px 7px; padding: 6px; font-size: 1.2em; width: 250px; }
        .ui-state-highlight { height: 1.2em; line-height: 1.2em; }
	</style>
        <div style =' border: 1px solid transparent; padding: 7px' class='errorMessageDiv'> </div>
<p><span class='ui-icon ui-icon-info' style='float:left; margin:0 7px 20px 0;'></span>Drag and drop teams between the two lists and rank them by clicking on and dragging a team to a new spot within the rankings list and the other teams will adjust to fit.</p>
<span id='unranked' class='ui-widget-content ui-corner-all'><h2>Unranked Teams</h2>".$unrankedTeams->output()."
           </span>

<span id='ranked' class='ui-widget-content ui-corner-all'><h2> Ranked Teams </h2>".$rankedTeams->output()."
   </span> ";
               
    }

    private function buildFeatureTeamListItem($countryName,$shortPicName)
    {
        $listItem = "<h3>$countryName</h3>";
        $rootpath = $this->parsesiteconfig->getRootPath();
        $loadType = $this->parsesiteconfig->getLoadType();
         $listItem .="<img src='$rootpath/themes/$loadType/global/images/banners/$shortPicName.png' alt='$countryName' />";
         return $listItem;
    }
    private function buildListAllFeauredTeams()
    {

        $featuresTeamsList = "<div><br/><br/><br/><h2>Featured Teams</h2>";
        $featuresTeamsList .= $this->buildFeatureTeamListItem("Australia", "aus_left");
      $featuresTeamsList .= $this->buildFeatureTeamListItem("India", "india_left");
         $featuresTeamsList .= $this->buildFeatureTeamListItem("Pakistan", "pak_left");
      $featuresTeamsList .= $this->buildFeatureTeamListItem("South Africa", "za_left");
        $featuresTeamsList .="</div>";
        return $featuresTeamsList;
    }

   private function checkNum($num){
  return ($num%2) ? TRUE : FALSE;
}


  







private function buildListAllTeams()
    {
        $listOfAllTeams = "<h2>List of All Cricket Playing Nations</h2>";
        $GameAdmin = new GameAdmin();
        $teamArray = $GameAdmin->getAllTeamsByName();

           $listOfAllTeams.= '<table width="100%" cellpadding="5" cellspacing="0">
            <tr>
                    <th align="left">Team</th>
                    <th width="75">Short Name</th>
                    <th width="40">Rank</th>
            </tr>';
           foreach ($teamArray as $key => $team) {
  if($this->checkNum($key) === FALSE)
  {
   $listOfAllTeams.="<tr bgcolor='#F3F3F3'>";

     }
  else
  {
    $listOfAllTeams .= "<tr>";
  }
  $listOfAllTeams .="<td>".$team['TEAM_POOL_NAME']."</td>
        <td>".$team['TEAM_POOL_SNAME']."</td>
        <td>";
   if ($team['TEAM_POOL_RANK'] < 1)
   {
     $listOfAllTeams.= "unranked</td></tr>";
   }
   else
   {
            $listOfAllTeams.= $team['TEAM_POOL_RANK']."</td></tr>";
   }
           }

           if (empty($teamArray))
           {
             $listOfAllTeams .= "<tr><td>No Teams Inside Dazzler</td></tr><tr><td>Please insert some cricket teams</td></tr>";
           }
          $listOfAllTeams.= "</table>";
        return $listOfAllTeams;
    }
    private function buildTeamManagementTopButtons()
    {
       $UserLogic = new UserLogic();
       $topButtons = NULL;
       if ($UserLogic->isUserCommentator())
       {
       $topButtons .= "<div id='teamNav'>";
        $topButtons .= "<h2>Teams</h2>";
        $topButtons .= $this->htmlelementsbuilder->buildButton("addTeam", "Add Team", "teamManagementButtons");
        $topButtons .= $this->htmlelementsbuilder->buildButton("updateTeamRank", "Set Team Ranks", "teamManagementButtons");
        $topButtons .= $this->htmlelementsbuilder->buildButton("updateTeam", "Update Team Details", "teamManagementButtons");
       $topButtons .= $this->htmlelementsbuilder->buildButton("deleteTeam", "Remove Team", "teamManagementButtons");
        $topButtons .= "<br/></div>";
       }

        return $topButtons;
    }


    private function buildDeleteTeamContent()
{
    $GameAdmin = new GameAdmin();
    $teamArray = $GameAdmin->getAllTeamsByName();
    $deleteTeamContent= '	<style>
	#feedback { font-size: 1.4em; }
	#deleteTeam-selectable .ui-selecting { background: #FECA40; }
	#deleteTeam-selectable .ui-selected { background: #F39814; color: white; }
	#deleteTeam-selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
	#deleteTeam-selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
	</style>';
    $deleteTeamContent .=    "
<p><span class='ui-icon ui-icon-info' style='float:left; margin:0 7px 20px 0;'></span>Select the desired team to be removed and click on delete team button to remove the team from dazzler.</p>";

    $deleteTeamContent .= "<p id='feedback'>
<span>Team selected to be deleted: </span><span id='select-result'>None</span>.
</p>";
    $deleteTeamContent .= '<ol id="deleteTeam-selectable">';

    foreach ($teamArray as $team) {
        $deleteTeamContent .= "<li class='ui-widget-content' name=".$team['TEAM_POOL_ID'].">".$team['TEAM_POOL_NAME']."</li>";
    }
     $deleteTeamContent .= "</ol>";
    return $deleteTeamContent;

}

    public function showDeleteTeams()
    {
        return $this->buildDeleteTeamContent();
    }

    public function showFeaturedTeamsList()
    {
        return $this->buildListAllFeauredTeams();
    }
    public function showTeamRankingList()
    {
        return $this->buildTeamRankingList();
    }

    public function showListAllTeams()
    {
        return $this->buildListAllTeams();
    }
    public function showTeamPage()
    {
        return $this->buildTeamManagementTopButtons();
    }

     public function showAddTeamForm()
    {
        return $this->buildAddTeamForm();
    }


}